package com.shujia.git;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Demo4Login {
    public static void main(String[] args) throws Exception {


        //1、输入用户名和密码


        //控制台输入流
        InputStream in = System.in;

        //包装流
        Scanner scanner = new Scanner(in);

        System.out.println("请输入用户名");
        String usernnmae = scanner.next();


        System.out.println("请输入密码");
        String password = scanner.next();

        System.out.println(usernnmae + "\t" + password);


        /*
         * 查询数据库判断用户名密码是否正确
         *
         */


        //1、加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        //建立连接
        Connection con = DriverManager.getConnection("jdbc:mysql://node3:3306/shujia", "root", "123456");

        // 通过用户名查询用户
        PreparedStatement stat = con.prepareStatement("select username,password from user where username=?");

        stat.setString(1, usernnmae);


        ResultSet resultSet = stat.executeQuery();


        //判断用户名是否存在
        if (!resultSet.next()) {
            System.out.println("用户名不存在");
            return;
        }

        //判断密码是否正确
        String newPassword = resultSet.getString("password");

        if (!newPassword.equals(password)) {
            System.out.println("密码不对");
            return;
        }


        System.out.println("登录成功");


    }
}
